package com.vlada.studzad.pages;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;

import com.vlada.studzad.model.Privilege;
import com.vlada.studzad.model.User;
import com.vlada.studzad.util.Security;

/**
 * Start page of application studzad.
 */
public class Index
{
	private String userName;
	private String password;
	private Privilege privilege;
	@ApplicationState
	@Property
	private User user;
	
	@OnEvent
	Object onFormSubmit(){
		Class nextPage = null;
		User authenticatedUser = null;
		authenticatedUser = Security.authenticate(userName, password, privilege);
		if (authenticatedUser != null) {
			user = authenticatedUser;
			if(user.getPrivilege()==Privilege.ADMINISTRATOR)
				nextPage = Administrator.class;
			if(user.getPrivilege()==Privilege.EMPLOYER)
				nextPage = Employer.class;
			if(user.getPrivilege()==Privilege.STUDENT)
				nextPage = Student.class;
		} else {
			nextPage = Index.class;
		}
		return nextPage;
	}
	
	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserName() {
		return userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public Privilege getPrivilege() {
		return privilege;
	}
	
	public void setPrivilege(Privilege privilege) {
		this.privilege = privilege;
	}
	
	public Privilege getAdministrator(){
		return Privilege.ADMINISTRATOR;		
	}
	
	public Privilege getEmployer(){
		return Privilege.EMPLOYER;		
	}
	
	public Privilege getStudent(){
		return Privilege.STUDENT;		
	}
	
	
	
}
